package com.young.web.ec.web.interceptor;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter{
	
	private int openingTime;
	
	private int closingTime;

	public int getOpeningTime() {
		return openingTime;
	}

	public void setOpeningTime(int openingTime) {
		this.openingTime = openingTime;
	}

	public int getClosingTime() {
		return closingTime;
	}

	public void setClosingTime(int closingTime) {
		this.closingTime = closingTime;
	}
	@Override
	public boolean  preHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler) throws Exception {
		 Calendar cal = Calendar.getInstance();
	        int hour = cal.get(Calendar.HOUR_OF_DAY);
	        if (openingTime <= hour && hour < closingTime) {
	            return true;
	        }
	        response.sendRedirect("http://baidu.com");
	        return false;
	}

}
